package com.yesorno.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yesorno.activity.R;
import com.yesorno.constants.Constants;
import com.yesorno.utils.BitmapUtils;

public class TopicLinerLayout extends LinearLayout {
	String checkBoxName;
	public int checkBoxID;
	int checkBoxFontSize = 11;// checkbox的文字的大小
	int checkBoxMarginTop = 10;// checkbox距离上面图片的高度
	public CheckBox checkBox;

	public TopicLinerLayout(Context context, int resourceDrawId,
			String checkBoxName, int checkBoxID) {
		super(context);
		LinearLayout.LayoutParams lp = new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		lp.weight = 1;
		this.setLayoutParams(lp);
		this.setGravity(Gravity.LEFT);
		this.setOrientation(VERTICAL);
		this.checkBoxName = checkBoxName;
		this.checkBoxID = checkBoxID;
		createImage(context);
		createCheckBox(context);
	}

	public TopicLinerLayout(Context context) {
		super(context);
	}

	public TopicLinerLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	// 创建checkbox多选
	private void createCheckBox(Context context) {
		checkBox = new CheckBox(context);
		checkBox.setText(checkBoxName);
		checkBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, checkBoxFontSize);
		checkBox.setBackgroundResource(R.drawable.checkbox_background);
		checkBox.setButtonDrawable(R.drawable.checkbox);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		lp.setMargins(0, checkBoxMarginTop, 0, 0);
		checkBox.setLayoutParams(lp);
		this.addView(checkBox);
	}

	// 创建logo图标
	private void createImage(Context context) {
		Bitmap viewBgrnd = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.common_type_logo);
		ImageView imageView = new ImageView(context);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				Constants.TOPIC_LOGO_WIDTH, Constants.TOPIC_LOGO_HEIGHT);
		imageView.setLayoutParams(lp);
		imageView.setDrawingCacheEnabled(true);
		imageView.setPadding(2, 2, 2, 2);
		imageView.setBackgroundResource(R.drawable.corner_logo_bg);
		Bitmap temp = BitmapUtils.getRoundedCornerBitmap(viewBgrnd);
		imageView.setImageBitmap(temp);
		this.addView(imageView);
	}

}
